$(function(){
    var kmdmclickRowIndex = null;
    $('#kmdmDataGrid').datagrid({
        url:$.webPath() + "/cwgl/kmdm/kmdmList?",
        toolbar:"#kmdmToolBar",
        fitColumns: true,
        singleSelect: true,
        pageNumber: 1,
        pagination: true,
        fit: true,
        rownumbers: true,
        columns:[[
            {field: 'code', title: '代码', width: 20},
            {field: 'pathname', title: '科目全称', width: 80},
            {field: 'zjm', title: '助记码', width: 20},
            {field: 'kmfx', title: '科目方向', width: 30},
            {field: 'kmdl', title: '科目大类', width: 50},
            {field: 'fzhslx', title: '辅助核算类型', width: 40},
            {field: 'qywl', title: '启用往来', width: 30}
        ]],
        onSelect: function (rowIndex, rowData) {
                var rows = $('#kmdmDataGrid').datagrid("getRows");
                if (kmdmclickRowIndex == rowIndex)
                   return;
                 kmdmclickRowIndex = rowIndex;
                var id = rowData.id;
                var detailPanel = $("#kmdm_layout").layout("panel", "center");
                $(detailPanel).panel("open");
                $(detailPanel).panel({href: $.webPath() + "/cwgl/kmdm/kmdm_detail?kmId=" + id });
        },
        onLoadSuccess: function () {
            var rows = $('#kmdmDataGrid').datagrid("getRows");
            if (rows.length > 0) {
                $('#kmdmDataGrid').datagrid("selectRow", 0);
            }else{
                var detailPanel = $("#kmdm_layout").layout("panel", "center");
                $(detailPanel).panel("close");
            }
        }
    });

    $("#kmdm_gsmc").combotree({
        onChange: function(oldValue,newValue){
            $("[name='kmdm_tree']").tree({
                url:$.webPath() + '/cwgl/kmdm/get_kmdmtreejson?gsId='+oldValue
            });
            kmdm_search();
        }
    });
    $("[name='kmdm_tree']").tree({
        url:$.webPath() + '/cwgl/kmdm/get_kmdmtreejson',
        onClick:function (node) {
            var code =  node.attributes.code;
            if(code && code !='null'){
                $("#kmdm_code").val(code);
                kmdm_search();
            }
        }
    });

    $("#kmdm_gsmc").combotree({
        url:$.webPath()+'/organ/gsjg/gsjg_tree?type=1',
        panelHeight:'200',
        lines:'true',
        loadFilter: function (rows) {
            return convert(rows);
        }
    });

    $("#kmdm_mod").click(function () {
        kmdm_modefy('修改科目代码', 'update_pjgl');
    });

    //修改科目代码
    function kmdm_modefy(title, action) {
        var row = $("#kmdmDataGrid").datagrid("getSelected");
        var gsId = $("#kmdm_gsmc").combotree("getValue");
        if (!row) {
            $.messager.alert("提示", "请选择一条记录更新!", "warning");
            return;
        }
        var href = $.webPath() + "/cwgl/kmdm/kmdm_content?kmId="+row.id;
        var actionUrl = $.webPath() + "/cwgl/kmdm/update_kmdm";
        kmdm_showDialog(title, href, actionUrl);
    }



    //科目代码导入
    $("#kmdm_inport").click(function(){
        var win = $.window({
            title:"科目代码导入",
            width:450,
            height:240,
            cache:false,
            url:$.webPath() + "/cwgl/kmdm/kmdm_upload",
            modal:true ,
            buttons:[
                {
                    text:'确认',
                    handler:function () {
                        $('#uploadKmdm').form('submit', {
                            onSubmit:function(){
                                var kmdmgsmc = $("#kmdm_gsmc_upload").combotree("getValue");
                                var upload = $("#upload").val();
                                if(!kmdmgsmc){
                                    $.messager.alert('提示', '请选择公司导入', 'error');
                                    return false;
                                }
                                var upload = upload.substring(upload.length-4,upload.length);
                                if(upload != ".xls"){
                                    $.messager.alert('提示', '请选择Excel文件导入', 'error');
                                    return false;
                                }
                                $(win).mask();
                            } ,
                            success:function (data) {
                                $(win).mask('hide');
                                if (data == '科目代码导入成功') {
                                    $(win).window('close');
                                    $('#kmdmDataGrid').datagrid('reload');
                                    $.messager.show({
                                        title:'提示',
                                        msg:data,
                                        timeout:3000,
                                        showType:'slide'
                                    });
                                } else
                                    $.messager.alert('提示', data, 'error');
                            }
                        });
                    }
                }
            ]
        });
    });

    //查询按钮
    $("#kmdm_search_btn").click(function () {
        kmdm_search();
    });
});

//查询科目代码
function kmdm_search() {
    var gsId = $("#kmdm_gsmc").combotree("getValue");
    if(gsId==null){
        gsId = $("#gsId").val();
    }
    $('#kmdmDataGrid').datagrid('load',
        {
            gsId: gsId  ,
            name:$("#kmdm_name").val(),
            code:$("#kmdm_code").val()
        }
    );

}

//弹出科目代码新增或修改的窗口
function kmdm_showDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:750,
        height:430,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'确认',
                handler:function () {
                    $(win).mask();
                    $('#kmdm_Form').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#kmdmDataGrid').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}



